package top.kaoshanji.q1ch07c;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 代码清单-q1ch07c-342页a：通过通道和缓冲来复制文件
 * 没有执行
 * author kaoshanji
 * ^_^
 * create time 2025/3/12 18:17
 */
public class ChannelCopy {

    private static final int BSIZE = 1024;

    public static void main(String[] args) {

        try (FileChannel in = new FileInputStream(args[0]).getChannel();
             FileChannel out = new FileInputStream(args[1]).getChannel()){

            ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
            while (in.read(buffer) != -1) {
                buffer.flip(); // 准备写
                out.write(buffer);
                buffer.clear(); // 准备读
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


}
